package com.serli.sample.data.runner.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.serli.sample.data.runner.domain.Runner;

public class RunnerService extends AbstractService {
		
	public RunnerService(EntityManager manager) {
		super(manager);
	}

	@SuppressWarnings("unchecked")
	public List<Runner> getAllOrderByFirstName() {
		List<Runner> result = new ArrayList<Runner>();
		result = manager.createNamedQuery(Runner.QUERY_FIND_ALL_ORDER_BY_FIRSTNAME).getResultList();
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Runner> getByFirstName(String firstName) {
		Query query = manager.createNamedQuery(Runner.QUERY_FIND_BY_FIRSTNAME);
		query.setParameter(Runner.PARAMETER_FIRSTNAME, firstName);
		return query.getResultList();
	}

	public boolean deleteByNames(String firstName, String lastName) {
		Query query = manager.createNamedQuery(Runner.QUERY_DELETE_BY_NAMES);
		query.setParameter(Runner.PARAMETER_FIRSTNAME, firstName);
		query.setParameter(Runner.PARAMETER_LASTNAME, lastName);
		return (query.executeUpdate() > 0)?true:false;
	}
}
